package com.wlt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wlt.mapper.CategoryMapper;
import com.wlt.pojo.Category;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
@DubboService
@Transactional
public class CategoryServiceImpl implements CategoryService
{
    private static final Logger log = LoggerFactory.getLogger(CategoryServiceImpl.class);
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    
    @Override
    public void add (Category category)
    {
        categoryMapper.insert(category);
        
        refreshRedisCategory();
    }
    
    @Override
    public void update (Category category)
    {
        categoryMapper.updateById(category);
        
        refreshRedisCategory();
    }
    
    @Override
    public void updateStatus (Long id, Integer status)
    {
        Category category = categoryMapper.selectById(id);
        category.setStatus(status);
        categoryMapper.updateById(category);
        
        refreshRedisCategory();
    }
    
    @Override
    public void delete (Long[] ids)
    {
        categoryMapper.deleteBatchIds(Arrays.asList(ids));
        
        refreshRedisCategory();
    }
    
    @Override
    public Category findById (Long id)
    {
        return categoryMapper.selectById(id);
    }
    
    @Override
    public Page<Category> search (int page, int size)
    {
        return categoryMapper.selectPage(new Page<>(page, size), null);
    }
    
    @Override
    public List<Category> findAll ()
    {
        // 1.从Redis中查询所有启用广告
        // 1.1 获取操作Redis中List数据的对象
        ListOperations<String, Category> listOperations = redisTemplate.opsForList();
        // 1.2 从Redis中获取所有启用的广告
        List<Category> categoryList = listOperations.range("categories", 0, -1);
        
        if (categoryList != null && !categoryList.isEmpty())
        {
            // 2.如果查到了结果，直接从Redis中返回
            log.info(" ---------------- 从Redis中查询到广告 --------------- ");
            
            return categoryList;
        }
        else
        {
            // 3.如果Redis中没有数据，则从数据库中查询并且同步到Redis
            log.info(" ---------------- 从数据库中查询到广告 --------------- ");
            
            QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("status", 1);
            List<Category> categories = categoryMapper.selectList(queryWrapper);
            
            // 同步到Redis中
            listOperations.leftPushAll("categories", categories);
            
            return categories;
        }
    }
    
    /**
     * 更新Redis中的广告数据
     */
    public void refreshRedisCategory ()
    {
        // 从数据库中查询广告
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 1);
        List<Category> categories = categoryMapper.selectList(queryWrapper);
        
        // 删除之前Redis中原有的广告数据
        redisTemplate.delete("categories");
        
        // 将新的广告数据同步到Redis
        ListOperations<String, Category> listOperations = redisTemplate.opsForList();
        listOperations.leftPushAll("categories", categories);
    }
}
